﻿package com.dykj.platform.iadapter.tags;

import java.io.IOException;
import java.io.Writer;

import org.apache.struts2.components.Component;
import java.util.logging.Logger;

import com.dykj.platform.iadapter.common.Constants;
import com.dykj.platform.iadapter.utils.StringUtils;
import com.opensymphony.xwork2.util.ValueStack;
import java.util.logging.Level;
import java.util.List;
import java.util.Map;
/**
 * 按钮标签类
 * @author Administrator
 *
 */
public class Button extends Component {
	private String name;

	private String method;

	private String value;

	private String check;

	@Override
	public boolean start(Writer arg0) {
		boolean result = super.start(arg0);
		StringBuffer str = new StringBuffer();
		try {
			/**
			 * 取得value的值
			 */
			if (value != null) {
				if (value.startsWith("%{") && value.endsWith("}")) {
					value = value.substring(2, value.length() - 1);
					value = StringUtils.objToStr(this.getStack().findValue(value));
				}
			} else {
				value = StringUtils.objToStr(this.getStack().findValue(
						"requestMsg['bn_value']"));
			}
			/**
			 * 取得name的值
			 */
			if (name != null) {
				if (name.startsWith("%{") && name.endsWith("}")) {
					name = name.substring(2, name.length() - 1);
					name = StringUtils.objToStr(this.getStack().findValue(name));
				}
			} else {
				name = StringUtils.objToStr(this.getStack().findValue(
						"requestMsg['bn_name']"));
			}
			/**
			 * 取得方法
			 */
			if (method != null) {
				if (method.startsWith("%{") && method.endsWith("}")) {
					method = method.substring(2, method.length() - 1);
					method = StringUtils.objToStr(this.getStack().findValue(method));
				}
			} else {
				method = StringUtils.objToStr(this.getStack().findValue(
						"requestMsg['bn_method']"));
			}
			if(name==null||name.startsWith("java.lang.Object")){
				name=method;
			}
			
			if (check != null) {
				if (check.startsWith("%{") && check.endsWith("}")) {
					check = check.substring(2, check.length() - 1);
					check = StringUtils.objToStr(this.getStack().findValue(check));
				}
			} else {
				check = StringUtils.objToStr(this.getStack().findValue(
						"requestMsg['bn_check']"));
			}
			if (!value.startsWith("java.lang.Object")
					&& !method.startsWith("java.lang.Object")) {
				str.append("<div class='operButton'>");
				String v[] = value.split(";");
				String n[] = name.split(";");
				String m[] = method.split(";");
				String c[] = null;
				if (!check.startsWith("java.lang.Object")) {
					c = check.split(";");
				}
				if (v.length == m.length) {
					boolean view = true;
					for (int i = 0; i < v.length; i++) {
						if (Constants.AUTH_BUTTON) {//验证按钮是否显示
							view = false;
							String action = StringUtils.objToStr(this.getStack()
									.findValue("requestMsg['action']"));
							if (action.startsWith("java.lang.Object")) {
								action = "";
							}
							String funStr = action + "!" + m[i] + ".action";
							Map map = (Map) this.getStack().findValue(
									"#session.userinfo");
							String type = map.get("type").toString();
							if ("0".equals(type) || "1".equals(type)) {
								view = true;
							} else {
								List list = (List) map.get("userFunction");
								for (int j = 0; j < list.size(); j++) {
									if (null != list.get(j)) {
										String fun = list.get(j).toString();
										if (fun.indexOf(funStr) > -1) { //验证是否有权限
											view = true;
										}
									}
								}
							}
						}
						if (view) {//开始拼接字符串
							str.append("<input class='button_2' value='");
							str.append(v[i]);
							if (!name.startsWith("java.lang.Object")) {
								str.append(" ' name='");
								str.append(n[i]);
								str.append("' id='");
								str.append(n[i]);
							}
							str.append("' onclick=formSubmitCheck('");
							str.append(m[i]);
							str.append("',");
							if (c != null && c[i].equals("true")) {
								str.append("true");
							} else {
								str.append("false");
							}
							str.append(") type='button'/>&nbsp;&nbsp;");
						}
					}
				} else {
					str.append("<input value='方法和值不一一对应'/>");
				}
				str.append("</div>");
			}
			arg0.write(str.toString());
		} catch (IOException e) {
			Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, e);
		}
		return result;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public Button(ValueStack arg0) {
		super(arg0);
	}
}
